﻿//======================================================================
//
//        Copyright : Zhengzhou Strawberry Computer Technology Co.,LTD.
//        All rights reserved
//        
//        Application:NFinal MVC framework
//        Filename :List.cs
//        Description :数据库表对象类
//
//        created by Lucas at  2015-6-30`
//     
//        WebSite:http://www.nfinal.com
//
//======================================================================
using System;
using System.Collections.Generic;
using System.Web;

namespace NFinal
{
    /// <summary>
    /// ORM返回表数据基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class List<T> : System.Collections.Generic.List<T>
    {
        public void AddNewField(string fieldName,Type t)
        {

        }
        /// <summary>
        /// 重写运算符
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static bool operator true(List<T> list)
        {
            if (list != null && list.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 重写运算符
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static bool operator false(List<T> list)
        {
            if (list != null && list.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 写入json
        /// </summary>
        /// <param name="tw">文本写入对象</param>
        /// <param name="addBracket">是否在两边添加大括号</param>
        public void WriteJson(System.IO.TextWriter tw,bool addBracket=true)
        {
            if (this == null)
            {
                tw.Write("null");
            }
            else
            {
                if (addBracket)
                {
                    tw.Write("[");
                }
                bool isFirst = true;
                foreach (NFinal.JsonInterface str in this)
                {
                    if (!isFirst)
                    {
                        tw.Write(",");
                    }
                    if (isFirst)
                    {
                        isFirst = false;
                    }
                    str.WriteJson(tw);
                }
                if (addBracket)
                {
                    tw.Write("]");
                }
            }
        }
        /// <summary>
        /// 把些表对象转换为Json字符串
        /// </summary>
        /// <param name="addBracket">是否在两边添加大括号</param>
        /// <returns></returns>
        public string ToJson(bool addBracket = true)
        {
            if (this)
            {
                System.IO.StringWriter tw = new System.IO.StringWriter();
                this.WriteJson(tw,addBracket);
                string result = tw.ToString();
                tw.Close();
                return result;
            }
            else
            {
                return "null";
            }
        }
    }

}